package com.tataben.db.util;

import com.tataben.db.DLLogger;
import com.tataben.db.table.DLColumn;
import com.tataben.db.table.DLTable;
import com.tencent.wcdb.Cursor;

import java.util.HashMap;

/**
 * 数据库游标转换成实体类
 */
public final class DLCursorUtils {
    public static <T> T getEntity(DLTable<T> table, final Cursor cursor) throws Throwable {
        T entity = table.createEntity();
        HashMap<String, DLColumn> columnMap = table.getColumnMap();
        int columnCount = cursor.getColumnCount();
        for (int i = 0; i < columnCount; i++) {
            String columnName = cursor.getColumnName(i);
            DLColumn column = columnMap.get(columnName);
            if (column != null) {
                column.setValueFromCursor(entity, cursor, i);
            }
        }
        return entity;
    }
    public static void closeQuietly(Cursor cursor) {
        if (cursor != null) {
            try {
                cursor.close();
            } catch (Throwable ignored) {
                DLLogger.d(ignored.getMessage(), ignored);
            }
        }
    }
}